﻿using System;
using System.Collections.Generic;
using System.Text;
using hedefgrup.reports.renault.date;

namespace hedefgrup.reports.common.SSRD
{
    public class Criteria_SSRD_SSMPeriod : Criteria
    {
        public const string PleaseSelect = "Satış Tarihine Göre Seçim";

        public Criteria_SSRD_SSMPeriod()
        {
            Values.Add(PleaseSelect);
        }

        public override string Name
        {
            get
            {
                return "SSM Period";
            }
        }

        public override string[] GetValueList()
        {
            List<string> values = new List<string>();
            Tree ssmRoot = Context.Project.GetChild("SSMPeriods");
            foreach (Tree child in ssmRoot.Children)
            {
                values.Add(child.Text);
            }
            values.Add(PleaseSelect);

            values.Reverse();

            return values.ToArray();
        }

        public reportMonth GetBeginMonth()
        {
            if (Value == PleaseSelect)
            {
                return null;
            }
            Tree ssmRoot = Context.Project.GetChild("SSMPeriods");
            foreach (Tree child in ssmRoot.Children)
            {
                if (child.Text == Value)
                {
                    return new reportMonth(child["BeginDate"]);
                }
            }
            throw new Exception("The BeginDate attribute of SSM Period " + Value + " could not be found");
        }

        public reportMonth GetEndMonth()
        {
            if (Value == PleaseSelect)
            {
                return null;
            }
            Tree ssmRoot = Context.Project.GetChild("SSMPeriods");
            foreach (Tree child in ssmRoot.Children)
            {
                if (child.Text == Value)
                {
                    return new reportMonth(child["EndDate"]);
                }
            }
            throw new Exception("The EndDate attribute of SSM Period " + Value + " could not be found");
        }

        public override object[] ToSqlStatementValues()
        {
            return null;

            if (Value != PleaseSelect)
            {
                Tree ssmRoot = Context.Project.GetChild("SSMPeriods");
                return new object[]
                {
                    GetBeginMonth(),
                    GetEndMonth()
                };
            }
            else
            {
                return null;
            }
        }

        public override string ToSqlStatementFilter(ref int parameterOffset)
        {
            if (Value != PleaseSelect)
            {
                if (Report.GetCriteriaByName("Begin Month") != null)
                {
                    Report.GetCriteriaByName("Begin Month").Values[0] = GetEndMonth().ToYYYYMM();
                }
                if (Report.GetCriteriaByName("End Month") != null)
                {
                    Report.GetCriteriaByName("End Month").Values[0] = GetEndMonth().ToYYYYMM();
                }
                if (Report.GetCriteriaByName("Roll") != null)
                {
                    Report.GetCriteriaByName("Roll").Values[0] = (GetEndMonth().getMonthYearValue() - GetBeginMonth().getMonthYearValue() + 1).ToString();
                }
            }
            return null;
        }

        public override string Expression
        {
            get { throw new NotImplementedException(); }
        }

        public override string Operator
        {
            get { throw new NotImplementedException(); }
        }

        public override Type ValueType
        {
            get { return typeof(int); }
        }
    }
}
